﻿using System;
using System.Data;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

public class Utils
{
    // Random number
    private int GetRandomNum(int minValue, int maxValue)
    {
        Random rand = new Random();
        return rand.Next(minValue, maxValue + 1);
    }

	// Convert DateTime dd/MM/yyyy to MM/dd/yyyy.
	public DateTime Convert_ddMMyyyy_MMddyyyy(string datein)
	{
		DateTime date = DateTime.ParseExact(datein, new string[] { "dd/MM/yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);
		string dateformat = string.Format("{0: MM/dd/yyyy}", date);
		DateTime dateout = DateTime.Parse(dateformat);
		return dateout;
	}

	// Format numeic VietNam.
	public string FormatNumber(int num)
	{
		string format = String.Format("{0:0,0}", num);
		string result = format.Replace(".", ",");
        if (result == "00")
            result = "0";
        return result;
	}

	// Format numeric VietNam in list Data.
	public static string FormatNumberList(object num)
	{
		if (num == null || num.ToString() == "")
			return null;
		else
		{
			int number = Int32.Parse(num.ToString());
			string format = String.Format("{0:0,0}", number);
			string result = format.Replace(".", ",");
            if (result == "00")
                result = "0";
            return result;
		}
	}

	// Format Datetime to dd/MM/yyyy.
	public string FormatDate(DateTime date)
	{
		string result = date.ToString("dd/MM/yyyy");
		return result;
	}


    // Format Datetime to MM/dd/yyyy.
    public string FormatDateMMDDYY(DateTime date)
    {
        string result = date.ToString("MM/dd/yyyy");
        return result;
    }

	// Format Datetime to dd/MM/yyyy in list Data.
	public static string FormatDateList(object date)
	{
		if (date == null || date.ToString() == "")
			return null;
		else
		{
			DateTime date_af = DateTime.Parse(date.ToString());
			string result = date_af.ToString("dd/MM/yyyy");
			return result;
		}
	}

    // Format Datetime to dd/MM/yyyy.
    public string FormatDateToTime(DateTime date)
    {
        string result = date.ToLongTimeString();
        return result;
    }

    // Format Datetime to dd/MM/yyyy in list Data.
    public static string FormatDateToTime_List(object date)
    {
        if (date == null || date.ToString() == "")
            return null;
        else
        {
            DateTime date_af = DateTime.Parse(date.ToString());
            string result = date_af.ToLongTimeString();
            return result;
        }
    }

	// Format Gender.
	public string FormatGender(string num)
	{
		string gender = "Nữ";
		if (num == "1")
			gender = "Nam";
		return gender;
	}

	// Format Gender in list Data.
	public static string FormatGender(object gender)
	{
		if (gender == null || gender.ToString() == "")
			return null;
		else
		{
			string result = "";
			string temp = gender.ToString();
			if (temp == "1")
				result = "Nam";
			else
				result = "Nữ";
			return result;
		}
	}

	// Encrypt MD5.
	public string HashMD5(string str)
	{
		UTF32Encoding utf = new UTF32Encoding();
		byte[] bytes = utf.GetBytes(str);
		MD5 md5 = new MD5CryptoServiceProvider();
		byte[] result = md5.ComputeHash(bytes);
		return Convert.ToBase64String(result);
	}

	// Return URL.
	public string ReturnURL(string temp)
	{
		string[] word = temp.Split('/');
		int i = word.Length;
		return word[i - 1].ToString();
	}

    public string escapeURL(string text)
    {
        string result = "";
        result = text.Trim();
        result = text.Replace(" ","-");
        return result;
    }

    public string descapeURL(string text)
    {
        string result = "";
        result = text.Trim();
        result = text.Replace("-"," ");
        return result;
    }

	public string ConvertToUnSign(string text)
	{
		for (int i = 32; i < 48; i++)
		{
			text = text.Replace(((char)i).ToString(), " ");        
        }        
		text = text.Replace(" ", "-");
		text = text.Replace(",", "");
		text = text.Replace(";", "");
		text = text.Replace(":", "");
        text = text.Replace("”", "");
        text = text.Replace("“", "");
        text = text.Replace("?", "");
        text = text.Replace("....", "");
        text = text.Replace("...", "");
        text = text.Replace("..", "");
        text = text.Replace(".", "");
        text = text.Replace("-----", "-");
        text = text.Replace("----", "-");
		text = text.Replace("---", "-");
		text = text.Replace("--", "-");

		Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
		string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
		return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
	}

    public string ConvertToUnSignProductKeyWord(string text)
    {
        for (int i = 32; i < 48; i++)
        {
            text = text.Replace(((char)i).ToString(), " ");
        }
        text = text.Replace(",", "");
        text = text.Replace(";", "");
        text = text.Replace(":", "");
        text = text.Replace("”", "");
        text = text.Replace("“", "");
        text = text.Replace("?", "");
        text = text.Replace("....", "");
        text = text.Replace("...", "");
        text = text.Replace("..", "");
        text = text.Replace(".", "");
        text = text.Replace("-----", " ");
        text = text.Replace("----", " ");
        text = text.Replace("---", " ");
        text = text.Replace("--", " ");

        Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
        string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
        return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
    }

    // Format Status
    public string FormatStatus(string status)
    {
        string result = "";
        if (status == "1")
            result = "Hiển thị";
        else
            result = "Không hiển thị";
        return result;
    }


	// Encrypt Keyword
	public static string EncryptKeyWord(string toEncrypt, string key, bool useHashing)
	{
		byte[] keyArray;
		byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

		if (useHashing)
		{
			MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
			keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
		}
		else
			keyArray = UTF8Encoding.UTF8.GetBytes(key);

		TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
		tdes.Key = keyArray;
		tdes.Mode = CipherMode.ECB;
		tdes.Padding = PaddingMode.PKCS7;

		ICryptoTransform cTransform = tdes.CreateEncryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

		return Convert.ToBase64String(resultArray, 0, resultArray.Length);
	}

	// Decrypt Keyword
	public static string DecryptKeyWord(string toDecrypt, string key, bool useHashing)
	{
		byte[] keyArray;
		byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

		if (useHashing)
		{
			MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
			keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
		}
		else
			keyArray = UTF8Encoding.UTF8.GetBytes(key);

		TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
		tdes.Key = keyArray;
		tdes.Mode = CipherMode.ECB;
		tdes.Padding = PaddingMode.PKCS7;

		ICryptoTransform cTransform = tdes.CreateDecryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

		return UTF8Encoding.UTF8.GetString(resultArray);
	}

    public int IsNullConvertInt32(string input, int num)
    {
        int result = 0;
        if (input == "" || input == null)
            result = num;
        else
            result = Convert.ToInt32(input);
        return result;
    }
}